#!/usr/bin/env python
# -*- coding:utf8 -*-
"""
Author : mafei
Date   : 2019-12-15
"""
import rpyc
from mmonitor.common.config import rpc_port, rpc_host

class RpcClient(object):
    def __init__(self):
        self.conn = rpyc.connect(rpc_host, rpc_port)
        self.spider_func = 'mmonitor.main.spider:run_job'

    def add_job(self, url_id, minute):
        """
         minute
         seconds
        """
        job = self.conn.root.add_job(self.spider_func, 'interval',
                                args=[url_id], seconds=minute)
        return job

    def get_jobs(self):
        jobs = self.conn.root.get_jobs()
        return jobs

    def remove_job(self, job_id):
        try:
            return self.conn.root.remove_job(job_id)
        except Exception:
            return 0

    def pause_job(self, job_id):
        try:
            return self.conn.root.pause_job(job_id)
        except Exception:
            return 0

    def resume_job(self, job_id):
        try:
            return self.conn.root.resume_job(job_id)
        except Exception:
            return 0


if __name__ == "__main__":
    rpc_client = RpcClient()


    # job_id = rpc_client.add_job("",2)
    # print(job_id.id)
    for i in rpc_client.get_jobs():
        rpc_client.remove_job(i.id)